package com.hsy.sunflowerbao.base.ui.fragment

import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Message
import com.flux.dispatcher.RxViewDispatch
import com.flux.ui.fragment.FBaseFragment
import com.flux.utils.LogUtil
import com.hsy.sunflowerbao.Application
import com.hsy.sunflowerbao.base.store.BaseStore

/**
 * 基础Fragment
 * Created on 2017/12/4.
 *
 * @author Yang
 */
abstract class BaseFragment<T : BaseStore> : FBaseFragment(), RxViewDispatch {

    protected val dispatcher = Application.instance.rxFlux.dispatcher
    protected val manager = Application.instance.rxFlux.subscriptionManager
    @Suppress("UNCHECKED_CAST")
    protected abstract var store: T

    open fun onFragmentRefresh() {

    }

    override fun onHandleMessage(msg: Message) {

    }

    override fun initData() {
        store.register()
        dispatcher.subscribeRxView(this)
        onFragmentRefresh()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        store.unregister()
        dispatcher.unsubscribeRxView(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        System.gc()
    }

    override fun getRxTag(): String {
        return javaClass.simpleName
    }

    /**
     * 显示列表对话框
     *
     * @param items    列表项
     * @param listener 点击监听
     */
    protected fun showListDialog(items: Array<String>, listener: DialogInterface.OnClickListener) {
        AlertDialog.Builder(attachedActivity)
                .setItems(items, listener)
                .setCancelable(true)
                .show()
                .setCanceledOnTouchOutside(true)
    }
}